<template>
	<view class="searchLayout pageBg">
		<view class="search">
			<uni-search-bar 
			@confirm="onSearch"
			@cancel="onClear"
			@clear="onClear"
			focus 
			placeholder="搜索"
			v-model="queryParams.name">
			</uni-search-bar>
		</view>
		
		<view v-if="flg">
			<view v-if="!classList.length || noSearch">
				<view class="history" v-if="historySearch.length">
					<view class="topTitle">
						<view class="text">最近搜索</view>
						<view class="icon" @click="removeHistory">
							<uni-icons type="trash" size="25"></uni-icons>
						</view>
					</view>
					<view class="tabs">
						<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>		
					</view>
				</view>
				
				<view class="recommend">
					<view class="topTitle">
						<view class="text">热门搜索</view>				
					</view>
					<view class="tabs">				
						<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
					</view>
				</view>
			</view>
		</view>
		<scroll-view scroll-x  class="navscroll" v-else>
					 <view class="item" :class="navIndex==0?'active':''" @click="clickNav(0,'')">课程</view>
					  <view class="item" :class="navIndex==1?'active':''" @click="clickNav(1,'')">教师</view>
			
		</scroll-view> 
		
		
		
		
		<view class="noSearch" v-if="navIndex==0">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"  v-if="course.length==0"></uv-empty>
		</view>
		
		<view class="noSearch" v-if="navIndex==1">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"  v-if="teachers.length==0"></uv-empty>
		</view>
		
		
		<view class="content">
				<view class="box"  v-for="item in course" :key="item.id" v-if="navIndex==0">
					<image :src="item.cover" mode="aspectFill"  @click="goToCoureseDetail(item.id)"></image>
					<view class="overlay">
						<view class="watch">
							<uni-icons type="videocam" color="white" size="20" >
								<text style="font-size: 14px;">{{item.viewCount}}</text>
							</uni-icons>
						</view>
						<view class="lessons">
							<text>共{{item.lessonNum}}课时</text>
						</view>
					</view>
					<view class="msg">
						<text>{{item.title}}</text>
						<uni-icons type="locked-filled" size="15"  color="red" v-if="item.price!=0">{{item.price}}￥</uni-icons>
						<text v-else style="color: red;">免费观看</text>
					</view>
				</view>
				
				<view class="teacherList" v-else>
					<view class="item" v-for="(item,index) in teachers" :key="item.id">
						<every-teacher :teacher="item"></every-teacher>
					</view>
				</view>
		</view>
		
		
		<!-- <view>
			 <view class="list">
				<navigator :url="`/pages/class/class?id=`+item.id"  class="item" 
				v-for="item in course" :key="item.id">				
					<image :src="item.bookIco" mode="aspectFill"></image>
					<text>{{item.bookName}}</text>				
				</navigator>
			</view>		
			 <view v-if="noData || classList.length"><uni-load-more :status="noData?'noMore':'loading'"/></view> 
		</view>
		 -->
		
	</view>
</template>

<script setup>
import {apiGetPopularCourse} from '@/apis/index.js'
import {apiGetTeachers} from '@/apis/teacher.js'
import {updateCourseViewCount} from '@/apis/course.js'
//查询参数
const queryParams = ref({	
	pageNum:1,
	pageSize:12,
	name:""
})

const flg=ref(true)

const teachers=ref([])
//搜索历史词
const historySearch = ref(uni.getStorageSync("historySearch")||[]);

//热门搜索词
const recommendList = ref(["java","vue","uniapp","mysql"]);

//没有更多
const noData = ref(false);
//没有搜索结果
const noSearch = ref(false);

//搜索结果列表
const classList = ref([]);

const navIndex=ref(0)

const courseVo=ref({
		 currentPage:1,
		 pageSize:12,
		 title:'',
		 id:'',
	})
	
const course=ref([])	
	
//点击搜索
const onSearch = ()=>{
	flg.value=false;
	console.log("进行了搜索");
	 uni.showLoading()
	historySearch.value=[...new Set([queryParams.value.name,...historySearch.value])].slice(0,10)
	uni.setStorageSync("historySearch",historySearch.value)
	console.log(queryParams.value.name);
	searchCourse()
	getTeachers(currentPage.value,pageSize.value)
}

//点击清除按钮 
const onClear = ()=>{
    flg.value=true  
	initParams();
}

const currentPage=ref(1)
const pageSize=ref(12)

const clickNav=async (index)=>{
	navIndex.value=index
	
	console.log("==========clickNav点击了=============");
	console.log('index',index);
	
		
	

	
	 
	
	
	
}

//点击标签进行搜索
const clickTab = (value)=>{
	queryParams.value.name=value
	flg.value=false;
	historySearch.value=[...new Set([queryParams.value.name,...historySearch.value])].slice(0,10)
	uni.setStorageSync("historySearch",historySearch.value)
	searchCourse()
	getTeachers(currentPage.value,pageSize.value)
	
}


//点击清空搜索记录
const removeHistory = ()=>{
	uni.showModal({
		title:"是否清空历史搜索？",
		success:res=>{
			if(res.confirm){
				uni.removeStorageSync("historySearch")
				historySearch.value=[]
				console.log("确认删除");			
			}
		}
	})
}

const initParams=()=>{
	course.value=[]
	teachers.value=[]
	noData.value=false
	noSearch.value=false
	flg.value=true
	navIndex.value=0
	const queryParams = ref({
		pageNum:1,
		pageSize:12,
		name:""
	})
}

//触底加载更多
onReachBottom(()=>{
	
})

//关闭有页面
onUnload(()=>{
	uni.hideLoading();
})

const searchCourse= async ()=>{
		courseVo.value.title=queryParams.value.name
		let res=await apiGetPopularCourse(courseVo.value)
		if(res.data.data!=null){
			course.value=res.data.data.records
			
			
			console.log("======搜索的课程==========")
			console.log(course.value);
		}else{
			course.value=[]
			
		}
		uni.hideLoading()
}


const goToCoureseDetail=(id)=>{
		console.log("===============id==============");
		console.log(id);
		updateViewCount(id)
		uni.navigateTo({
			url:"/pages/course/course?id="+id
		})
	}
	
const updateViewCount=async (id)=>{
		let res=await updateCourseViewCount(id)
		console.log("更新浏览量成功");
}		

const getTeachers = async (pageCurrent, pageSize) => {
	    let teacherVo={
			name:queryParams.value.name,
		}
		const res = await apiGetTeachers(pageCurrent,pageSize,teacherVo)
		//teachers.value = [...teachers.value, ...res.data.data.records]
		if(res.data.data!=null){
			teachers.value=res.data.data.records
			
			
			console.log("======搜索的课程==========")
			console.log(teachers.value);
		}else{
			teachers.value=[]
			
		}
		uni.hideLoading()
		
	}
</script>

<style lang="scss" scoped>
.searchLayout{
	.navscroll{
		       
				height: 100rpx;
				background: #F7F8FA;
				white-space: nowrap;
				position: fixed;
				top:200rpx;
				left: 0;
				::v-deep ::-webkit-scrollbar{
					width: 4px !important;
					height:1px !important;
					overflow: auto !important;
					background: transparent !important;
					-webkit-appearance: auto !important;
					display: block;
				}
			
				
				.item{
					font-size: 40rpx;
					display: inline-block;
					line-height:100rpx;
					padding: 0 30rpx;
					color: #333;
					&.active{
						color: #31c27c;
					}
				}
				
		} 
		
	.search{
		padding:0 10rpx;
	}
	.topTitle{
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 32rpx;
		color:#999;
	}
	.history{
		padding:30rpx;		
	}
	.recommend{
		padding:30rpx;
	}
	.tabs{
		display: flex;		
		align-items: center;
		flex-wrap: wrap;
		padding-top:20rpx;
		.tab{
			background: #F4F4F4;
			font-size: 28rpx;
			color:#333;
			padding:10rpx 28rpx;
			border-radius: 50rpx;
			margin-right: 20rpx;
			margin-top: 20rpx;
		}
	}	
	.list{
		display: grid;
		grid-template-columns: repeat(3,1fr);
		gap: 5rpx;
		padding:20rpx 5rpx;		
		.item{
			height: 440rpx;
			image{
				width: 100%;
				height: 90%;
				display: block;
			}			
		}		
	}
	
	
	.content{
		width: 1000rpx;
	
		margin-top:70rpx;
		
			.box{
				width: 360rpx;
				height: 350rpx;
				display:inline-block;
				margin-right:15rpx;
				position: relative;
				image{
					width: 100%;
					height: 100%;
				
				}
				.overlay {
				  width: 100%;
				  position: absolute;
				  bottom: 10rpx; 
				  background-color: rgba(0, 0, 0, 0.5); 
				  padding: 5rpx 10rpx;
				  border-radius: 5rpx;
				  color: white;
				  display: flex;
				  align-items: center;
				 .watch{
				  	  display: flex;
				  	  align-items: center;
					  
				  } 
				  .lessons {
				    margin-left: 100rpx; 
				  }
				}
				
				.msg{
					width: 100%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;				
				}
				
				
			}
			.teacherList {
				padding: 0 30rpx;
				display: grid;
				grid-template-columns: repeat(2, 1fr);
				grid-template-rows: repeat(3, 1fr);
				grid-gap: 20rpx;
			
				.item {
					width: 250rpx;
					height: 300rpx;
					margin: 50rpx auto;
				}
			}
		
	}
}
</style>